package org.client.ui;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;

import org.client.tools.ClientTool;
import org.pojo.User;
import org.pojo.Message.Message;

public class IMMemberList {
	public int length;
	public List<IMJLabel> label;
	public IMMemberList() {
		// TODO Auto-generated constructor stub
		label = new LinkedList<IMJLabel>();
	}
	public void updateOnline(List<User> u,JPanel jp){
		IMJLabel temp;
		Iterator<User> it=u.iterator();
		if(u.size() == 1){		
			File wavFile = new File("music/login.wav");
			try{
				AudioInputStream ais = AudioSystem.getAudioInputStream(wavFile);
				Clip  clip = AudioSystem.getClip();
				clip.open(ais);
				clip.start();
			}catch(Exception e){
				
			}
			
			User use = it.next(); 			
			temp = new IMJLabel(use.getNickname(), new ImageIcon("images/" +use.getPhoto()),JLabel.LEFT);
			temp.userid = use.getUserid();
			temp.addMouseListener(new flListener());
			label.add(temp);
			jp.add(temp); 
		}
		else{
			while(it.hasNext()){
				User use = it.next();          
				temp = new IMJLabel(use.getNickname(), new ImageIcon("images/" +use.getPhoto()),JLabel.LEFT); 
				temp.userid = use.getUserid();
				temp.addMouseListener(new flListener());
				label.add(temp);
				jp.add(temp);
			}
		}
	}
	public void updateOffLine(User u,JPanel jp){
		int i = 0;
		Iterator<IMJLabel> it=label.iterator();
		while(it.hasNext()){
			IMJLabel la = it.next(); 
			if(la.userid == u.getUserid()){
				label.remove(la);
				jp.remove(i);
				break;
			}
			i++;
		}
	}
	public void updateExist(JPanel jp){
		IMJLabel temp;
		Iterator<User> local=ClientTool.onlinefriends.iterator();
		label.removeAll(label);
		jp.removeAll();
		while(local.hasNext()){
			User u2 = local.next();          
			temp = new IMJLabel(u2.getNickname(), new ImageIcon("images/" +u2.getPhoto()),JLabel.LEFT); 
			temp.userid = u2.getUserid();
			temp.addMouseListener(new flListener());
			label.add(temp);
			jp.add(temp);
		}
	}
	public void AcceptChat(Message m){
		Iterator<IMJLabel> it=label.iterator();
		while(it.hasNext()){
			IMJLabel la = it.next(); 
			if(la.userid == m.getSenderId()){
				AcceptChatThread at = new AcceptChatThread(la,m);
				at.start();
				break;
			}
		}
	}
	class IMJLabel extends JLabel{
		public int userid;
		public boolean isThread;
		IMJLabel(String arg0, Icon arg1, int arg2){
			super(arg0, arg1, arg2);
			isThread = false;
		}
	}
	class flListener extends MouseAdapter
	{
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			IMJLabel lb = (IMJLabel) e.getSource();
			if (e.getClickCount() == 2) {
				System.out.println("进入聊天界面！");
				int id = lb.userid;
				if(ClientTool.clientviews.get(id)!=null){
					System.out.println("窗口已经打开！");
					return;
				}
				Iterator<User> it=ClientTool.onlinefriends.iterator();
				while(it.hasNext()){
					User use = it.next();  
					if(use.getUserid() == id){
						
						//创建聊天窗口
						
						IMChatView chat = new IMChatView(use);
						ClientTool.clientviews.put(id, chat);
						break;
					}
				}
			}
			else if (e.getClickCount() == 1) {
				lb.setForeground(Color.ORANGE);
			}
		}
		public void mouseExited(MouseEvent e) {
			IMJLabel lb = (IMJLabel) e.getSource();
			lb.setForeground(Color.BLACK);
		}
		public void mouseEntered(MouseEvent e) {
			IMJLabel lb = (IMJLabel) e.getSource();
			lb.setForeground(Color.BLUE);
		}
	}
	class AcceptChatThread extends Thread{
		private IMJLabel lb;
		private Message m;
		public AcceptChatThread(IMJLabel lb,Message m){
			this.lb = lb;
			this.m = m;
		}
		public void run()
		{
			int i = 0;
			if(lb.isThread) while(ClientTool.clientviews.get(lb.userid)==null);
			lb.isThread = true;
			while(ClientTool.clientviews.get(lb.userid)==null){
				if(i==0){
					lb.setForeground(Color.ORANGE);
					i=1;
				}else if(i==1){
					lb.setForeground(Color.BLACK);
					i=2;
				}else if(i==2){
					lb.setForeground(Color.BLUE);
					i=0;
				}
				try {
					sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			lb.isThread = false;
			Iterator<User> it=ClientTool.onlinefriends.iterator();
			while(it.hasNext()){
				User use = it.next();  
				if(use.getUserid() == lb.userid){
					IMChatView chat = ClientTool.clientviews.get(lb.userid);
					chat.showMessage(m);
					break;
				}
			}
			
			
		}
	}
}
